package com.martini.satoken01.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.martini.satoken01.entity.SysMessageBoarder;
import com.martini.satoken01.enums.PinBackgroundTypes;
import com.martini.satoken01.model.AjaxJson;
import com.martini.satoken01.model.vo.SysMessageBoarderVO;
import com.martini.satoken01.service.system.MessageBoarderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author martini at 2021/12/27 21:49
 */
@Slf4j
@RestController
@RequestMapping("/msgBoarder")
public class MessageBoarderController {

    private final MessageBoarderService messageBoarderService;

    public MessageBoarderController(MessageBoarderService messageBoarderService) {
        this.messageBoarderService = messageBoarderService;
    }

    @GetMapping
    @SaCheckLogin
    public AjaxJson getAreaBoarders(Integer offsetX, Integer offsetY, Integer width, Integer height) {
        List<SysMessageBoarder> boarderList =
            messageBoarderService.findByRect(offsetX, offsetY, offsetX + width, offsetY + height);

        long myId = StpUtil.getLoginIdAsLong();
        List<SysMessageBoarderVO> boarderVoList = boarderList.stream().map(i -> {
            SysMessageBoarderVO boarderVO = new SysMessageBoarderVO();
            BeanUtils.copyProperties(i, boarderVO);
            boarderVO.setIsMe(Objects.equals(boarderVO.getUserId(), myId));
            return boarderVO;
        }).collect(Collectors.toList());
        return AjaxJson.success().setData(boarderVoList);
    }

    @PostMapping("/insertOne")
    @SaCheckLogin
    public AjaxJson insertOne() {
        SysMessageBoarder boarder = new SysMessageBoarder();
        boarder.setContent("")
            .setCreateTime(LocalDateTime.now())
            .setLeft(300)
            .setTop(300)
            .setUserId(StpUtil.getLoginIdAsLong())
            .setPinBackgroundType(PinBackgroundTypes.COLOR.getCode())
            .setPinBackgroundColor("#ffd17f")
            .setHeaderBackgroundColor("purple")
            .setBodyBackgroundColor("skyblue")
            .setIsDeleted(false);
        messageBoarderService.insertOne(boarder);

        // TODO: 抽离出接口
        SysMessageBoarderVO boarderVO = new SysMessageBoarderVO();
        boarderVO.setIsMe(true);
        BeanUtils.copyProperties(boarder, boarderVO);
        return AjaxJson.success().setData(boarderVO).setMsg("生成成功");
    }

    @PostMapping("/updateOne")
    @SaCheckLogin
    public AjaxJson updateOne(@RequestBody SysMessageBoarder sysMessageBoarder) {
        int updated = messageBoarderService.updateOne(sysMessageBoarder);
        return updated > 0 ? AjaxJson.success().setMsg("修改成功") : AjaxJson.fail().setMsg("修改失败");
    }

    @PostMapping("/deleteOneById")
    @SaCheckLogin
    public AjaxJson deleteOneById(@RequestParam Long id) {
        // 首先根据id查找这个留言是否存在
        SysMessageBoarder boarder = messageBoarderService.findById(id);
        if (boarder == null) {
            return AjaxJson.fail().setMsg("没有找到改条留言，可能已经被删除");
        }
        long myId = StpUtil.getLoginIdAsLong();
        if (boarder.getUserId() != null && boarder.getUserId() != myId) {
            return AjaxJson.fail().setMsg("留言用户信息不匹配，删除失败");
        }

        // 删除
        int updated = messageBoarderService.softDeleteOneById(boarder.getId());

        return updated > 0 ? AjaxJson.success().setMsg("删除成功") : AjaxJson.fail().setMsg("删除失败，请联系管理员");
    }
}
